package cn.wgx.common.util;

import cn.wgx.common.base.BaseEntity;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.io.Serializable;
import java.util.*;

/**
 * controller 返回数据对象工具类
 * 包装成json返回,例如:
 * {
 *     code: 1,
 *     success: true,
 *     data: [],
 *     count: 100
 * }
 *
 *
 */
public class ResponseDataUtil {

    public static class BlankObject implements Serializable {}

    public static class ResponseData {
        private Integer code = 0;
        private String msg;
        private Object data;
        private Boolean success;
        private Long responseTime = System.currentTimeMillis();
    }

    public final static List EMPTY_LIST = new ArrayList<>();

    private final static BlankObject blankObject = new BlankObject();


    public static Map<String, Object> getInitMap(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "操作成功!");
//        map.put("count", 0);
        map.put("data", EMPTY_LIST);
        map.put("success", true);
        return map;
    }

    public static Map<String, Object> returnData(Object o){
        return returnData(o, null, null,null);
    }

    public static Map<String, Object> returnData(Object o, String key){
        return returnData(o, null, null,key);
    }

    public static Map<String, Object> returnData(Object o, Map map){
        return returnData(o, null, map,null);
    }

    public static Map<String, Object> returnData(Page page , Object o){
        return returnData(o, page, null,null);
    }

    public static Map<String, Object> returnData(Object o , Page page, Map map, String key){
        if(map == null) {
            map = getInitMap();
        }
        if(null == o){
            o = blankObject;
        }else if(o instanceof BaseEntity){
            BaseEntity oo = (BaseEntity)o;
            if(oo.isUpdate() && oo.get_result() != null){
                //o = blankObject;
                map.put("result", oo.get_result());
            }
        }else if(o instanceof Page){
            page = (Page)o;
            o = page.getRecords();
        }
        if(key == null){
            map.put("data", o);
        }else{
            map.put(key, o);
        }
        if(page != null){
            map.put("count", page.getTotal());
            map.put("pages", page.getPages());
        }else{
            if(o instanceof Collection){
                map.put("count",((Collection)o).size());
            }else if(o instanceof Integer){
                map.put("count", o);
            }
        }
        map.put("nowDate", System.currentTimeMillis());
        return map;
    }

    public static Map<String, Object> returnErrorMap(String errorMsg){
        return returnErrorMap(errorMsg, null, null);
    }
    public static Map<String, Object> returnErrorMap(String errorMsg, Integer code){
        return returnErrorMap(errorMsg, code, null);
    }
    public static Map<String, Object> returnErrorMap(String errorMsg, Map<String, Object> map){
        return returnErrorMap(errorMsg,null, map);
    }

    public static Map<String, Object> returnErrorMap(String errorMsg, Integer code, Map<String, Object> map){
        if(map == null) {
            map = getInitMap();
        }
        map.put("msg", errorMsg == null ? "error" : errorMsg);
        map.put("code", code == null ? -1 : code);
        map.put("success", false);
        return map;
    }

}
